package com.cskaoyan.controller.mall;

import com.cskaoyan.bean.bo.adminbo.AdminUpdatePasswordBo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketNoticeAdmin;
import com.cskaoyan.service.mall.ProfileService;
import com.cskaoyan.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin/profile")
public class ProfileController {

    @Autowired
    ProfileService profileService;

    @RequestMapping("lsnotice")
    public BaseRespVo queryNoticePage(BasePageInfo basePageInfo, String title, String type) {
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin primaryPrincipal = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
        CommonData<MarketNoticeAdmin> commonData = profileService.queryNotice(primaryPrincipal.getId(), basePageInfo, title, type);
        return BaseRespVo.succeed(commonData);
    }

    @RequestMapping("nnotice")
    public BaseRespVo queryNoticePage() {
        return BaseRespVo.succeed();
    }

    @RequestMapping("password")
    public BaseRespVo updatePassword(@RequestBody AdminUpdatePasswordBo adminUpdatePasswordBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin primaryPrincipal = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
        if (StringUtil.isEmpty(adminUpdatePasswordBo.getOldPassword()) || StringUtil.isEmpty(adminUpdatePasswordBo.getNewPassword()) || StringUtil.isEmpty(adminUpdatePasswordBo.getNewPassword2())) {
            return BaseRespVo.invalidParameter("输入不得为空！");
        }
        if (!adminUpdatePasswordBo.getNewPassword().equals(adminUpdatePasswordBo.getNewPassword2())) {
            return BaseRespVo.invalidParameter("新密码两次输入的不一致！");
        }
        int code = profileService.updatePassword(adminUpdatePasswordBo, primaryPrincipal.getId());
        if (code == 3) {
            return BaseRespVo.invalidParameter("原密码有误！");
        }
        if (code == 2) {
            return BaseRespVo.invalidParameter("新密码不得与原密码相同！请重新输入！");
        }
        if (code == 0) {
            return BaseRespVo.invalidParameter("修改失败！请重试！");
        }
        return BaseRespVo.succeed();
    }


}
